<template>
  <div class="JADP-common-layout">
    <div class="JADP-common-layout-center">
      <el-row class="JADP-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input v-model="query.realname" placeholder="请输入" clearable>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="职称/职务">
              <el-input
                v-model="query.technicaltitle"
                placeholder="请输入"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所任课程">
              <el-input
                v-model="query.teachingsubjects"
                placeholder="请输入"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="6">
              <el-form-item label="专职/兼职">
                <el-select
                  v-model="query.worktype"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in worktypeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="身份证号码">
                <el-input
                  v-model="query.certificatesnumber"
                  placeholder="请输入"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="所学专业">
                <el-input v-model="query.major" placeholder="请输入" clearable>
                </el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="6">
                <el-form-item label="技能等级">
                  <el-input
                    v-model="query.skilllevel"
                    placeholder="请输入"
                    clearable
                  >
                  </el-input>
                </el-form-item>
              </el-col> -->
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
              >
                展开
              </el-button>
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                v-else
              >
                收起
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JADP-common-layout-main JADP-flex-main">
        <div class="JADP-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button type="text" icon="el-icon-upload2" @click="exportData()"
              >导出
            </el-button>
            <el-button type="text" icon="el-icon-download" @click="uploadForm"
              >导入
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleBatchRemoveDel()"
              >批量删除
            </el-button>
          </div>
          <div class="JADP-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JADP-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull isContainer />
          </div>
        </div>
        <JADP-table
          v-loading="listLoading"
          class="JADP-common-table"
          :data="list"
          @sort-change="sortChange"
          has-c
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="realname" label="姓名" align="left" />
          <el-table-column prop="premium" label="是否推荐" align="left">
            <template #default="{ row }">
              <el-switch
                v-model="row.premium"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="switchChange(row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="gender" label="性别" width="0" align="left">
            <template v-slot="{ row }">
              {{ getDict("sex", row.gender) }}
            </template>
          </el-table-column>
          <el-table-column prop="birthday" label="出生年月" align="left" />
          <el-table-column
            prop="certificatesnumber"
            label="身份证号码"
            align="left"
            width="200"
          />
          <el-table-column prop="education" label="学历" width="0" align="left">
            <template v-slot="{ row }">
              {{ getDict("Education", row.education) }}
            </template>
          </el-table-column>

          <el-table-column
            prop="major"
            label="所学专业"
            align="left"
            show-overflow-tooltip
          />
          <!-- <el-table-column
              prop="major"
              label="所学专业"
              width="0"
              align="left"
            /> -->
          <el-table-column
            prop="worktime"
            label="工作时间"
            align="left"
            width="150"
            sortable="custom"
          />
          <el-table-column
            prop="technicaltitle"
            label="职称/职务"
            align="left"
            show-overflow-tooltip
          />
          <el-table-column
            prop="xzjob"
            label="职称专业"
            align="left"
            show-overflow-tooltip
          />
          <el-table-column
            prop="teachingsubjects"
            label="所任课程"
            align="left"
            show-overflow-tooltip
          />
          <el-table-column
            prop="fixedphone"
            label="安全培训教师岗位证书编号"
            width="200"
            align="left"
            show-overflow-tooltip
          />
          <!-- <el-table-column
              prop="safeworktime"
              label="从事安全生产培训工作时间"
              width="0"
              align="left"
              sortable="custom"
            /> -->
          <el-table-column prop="worktype" label="专(兼)职" align="left">
            <template #default="{ row }">{{
              row.worktype == 1 ? "专职" : row.worktype !== 0 ? "" : "兼职"
            }}</template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="250">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <el-button
                type="text"
                class="JADP-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
              <el-button type="text" @click="goDetail(scope.row.id)"
                >详情
              </el-button>

              <el-button
                type="text"
                @click="teacherRegistrationForm(scope.row.id)"
                >教师情况登记表
              </el-button>
            </template>
          </el-table-column>
        </JADP-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JADP-Form
      v-if="formVisible"
      ref="JADPForm"
      :premium="true"
      @refresh="refresh"
    />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
    <ImportBox
      v-if="importBoxVisible"
      ref="importBox"
      @refresh="reset()"
      @ImportPreviewApi="ImportPreviewApi"
      @importDataApi="importDataApi"
      @TemplateDownload="TemplateDownload"
      :URL="URL"
    />
    <A10_JSQKDJB v-if="JSQKDJBVisible" ref="JSQKDJB" @refresh="refresh" />
  </div>
</template>

<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import JADPForm from "./Form";
import ExportBox from "./ExportBox";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import Detail from "./Detail";
import A10_JSQKDJB from "./A10_JSQKDJB";
import {
  TemplateDownload,
  ImportData,
  ImportPreview,
} from "@/api/zazx/teacherManagement";
import A10_JSQKDJBVue from "../xy_da_classArchives/components/A10_JSQKDJB.vue";
export default {
  components: { JADPForm, ExportBox, Detail, A10_JSQKDJB },
  data() {
    return {
      showAll: false,
      detailVisible: false,
      // evaluationVisible: false,
      JSQKDJBVisible: false,
      query: {
        realname: undefined,
        technicaltitle: undefined,
        teachingsubjects: undefined,
        worktype: undefined,
        certificatesnumber: undefined,
        major: undefined,
        skilllevel: undefined,
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "realname", label: "姓名" },
        { prop: "gender", label: "性别" },
        { prop: "birthday", label: "出生年月" },
        { prop: "politicallandscape", label: "政治面貌" },
        { prop: "certificatesnumber", label: "身份证号码" },
        { prop: "education", label: "学历" },
        { prop: "major", label: "所学专业" },
        { prop: "worktime", label: "工作时间" },
        { prop: "technicaltitle", label: "技术职称" },
        { prop: "xzjob", label: "行政职务" },
        { prop: "skilllevel", label: "技能等级" },
        { prop: "teachingsubjects", label: "任教科目" },
        { prop: "telephone", label: "移动电话" },
        { prop: "safeworktime", label: "从事安全生产培训工作时间" },
        { prop: "unitname", label: "工作单位" },
      ],
      genderOptions: [],
      genderProps: { label: "fullName", value: "id" },
      educationOptions: [],
      educationProps: { label: "fullName", value: "id" },
      worktypeOptions: [
        { fullName: "专职", id: "1" },
        { fullName: "兼职", id: "2" },
      ],
      worktypeProps: { label: "fullName", value: "id" },
      importBoxVisible: false,
      URL: "teachersManage/Xy_ks_teachers/",
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
  },
  methods: {
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    switchChange(row) {
      request({
        url: "/api/teachersManage/Xy_ks_teachers/" + row.id,
        method: "PUT",
        data: { premium: row.premium },
      }).then((res) => {
        this.$message({
          message: res.msg,
          type: "success",
          duration: 1000,
          onClose: () => {
            this.visible = false;
            this.refresh(true);
          },
        });
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
        // premium:true,
        // 首页推荐 讲师推荐 固定值 中安组织id
        organizeId: "29eab438d531474f9159e60fbf5072ea",
      };
      request({
        url: `/api/teachersManage/Xy_ks_teachers/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/teachersManage/Xy_ks_teachers/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      const res = val.map((item) => item.id);
      this.multipleSelection = res;
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: "error",
          message: "请选择一条数据",
          duration: 1500,
        });
        return;
      }
      const ids = this.multipleSelection.join();
      this.$confirm("您确定要删除这些数据吗, 是否继续？", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/teachersManage/Xy_ks_teachers/batchRemove/${ids}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JADPForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/teachersManage/Xy_ks_teachers/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      this.JSQKDJBVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
    // 导入
    uploadForm() {
      this.importBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.importBox.init(this.columnList);
      });
    },
    ImportPreviewApi(val) {
      ImportPreview({ fileName: val })
        .then((res) => {
          console.log(res, "res");
          this.$refs.importBox.list = res.data.dataRow;
          this.$refs.importBox.btnLoading = false;
          this.$refs.importBox.active++;
        })
        .catch(() => {
          this.$refs.importBox.btnLoading = false;
        });
    },
    importDataApi(val) {
      ImportData({ list: val ,organizeId :this.$store.getters.organizeId})
        .then((res) => {
          this.$refs.importBox.result = res.data;
          this.$refs.importBox.resultList = res.data.failResult;
          this.$refs.importBox.btnLoading = false;
          this.$refs.importBox.active++;
        })
        .catch(() => {
          this.$refs.importBox.btnLoading = false;
        });
    },
    TemplateDownload() {
      //   TemplateDownload().then(res => {
      //     if (!res.data.url) return;
      //     window.location.href = this.define.comUrl + res.data.url;
      //   });
      window.location.href =
        this.define.comUrl +
        "/v3/api/testPaperManage/Xy_ks_examination_paper/teacher/templateDownload";
    },
    teacherRegistrationForm(id) {
      this.JSQKDJBVisible = true;
      this.$nextTick(() => {
        this.$refs.JSQKDJB.init(id);
      });
    },
  },
};
</script>
